﻿using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.ComponentModel;
using System.Data.EntityClient;
using System.Collections.Generic;
using System.Data.Objects.Internal;
using System.Data.Objects.DataClasses;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Collections;
using EntityModel;
using Common;

namespace DataAccess
{
    public class RetrieveHelper<T, U> : RetrieveHelperBase<T, U>
        where T : EntityBase, new()
        where U : UserModel
    {

        public RetrieveHelper()
            : base() { }

        public override T GetByKey(U userInfo, params object[] keys)
        {
            return this.GetOne<T>(userInfo, keys);
        }
        
        public override List<T> GetAll()
        {
            return this.GetRepositoryWithoutTracking<T>().ToList();
        }

        public override Result<T> GetAllPaging(string sortCol, SORT_ORDER sortOrder, Int16 currentPage, Int16 pageNumber)
        {
            var repEntity = this.GetRepositoryWithoutTracking<T>();            
            Result<T> objResult = new Result<T>();

            objResult.TotalRecords = repEntity.Count();
            objResult.EntityList = repEntity.Paging<T>(sortCol, sortOrder, currentPage, pageNumber).ToList();

            return objResult;
        }

        public override Result<T> GetAllPaging(Func<T, bool> whereExpression, string sortCol, SORT_ORDER sortOrder, Int16 currentPage, Int16 pageNumber)
        {
            var repEntity = this.GetRepositoryWithoutTracking<T>().Where(whereExpression).AsQueryable();
            Result<T> objResult = new Result<T>();

            objResult.TotalRecords = repEntity.Count();
            objResult.EntityList = repEntity.Paging<T>(sortCol, sortOrder, currentPage, pageNumber).ToList();

            return objResult;
        }

        public override Result<T> GetComplexPaging(IQueryable<T> source, string sortCol, SORT_ORDER sortOrder, Int16 currentPage, Int16 pageNumber)
        {
            Result<T> objResult = new Result<T>();

            objResult.TotalRecords = source.Count();
            objResult.EntityList = source.Paging<T>(sortCol, sortOrder, currentPage, pageNumber).ToList();

            return objResult;
        }

        public override List<T> Search(T entity)
        {
            return null;
        }
    }
}
